#include "mainwindow.h"
#include "component/window.h"
#include "statusbar/StatusBar.h"
#include "title/TitleBar.h"
#include <QLabel>
#include <qapplication.h>
#include <qguiapplication.h>
#include <qnamespace.h>
#include <qpoint.h>
#include <qscreen.h>
#include <qsize.h>
#include <qsplitter.h>
#include <qwindowdefs.h>
#include <QGuiApplication>
#include <QScreen>

MainWindow::MainWindow(QWidget *parent) : Window(parent)
{
    resize(1200, 740);
    setWindowFlags(Qt::FramelessWindowHint);
    Window::setIsWindowMax(false);

    initTitleBar();
    initStatusBar();
    initCenterContext();
    initDockWidget();
}

MainWindow::~MainWindow() {}

void MainWindow::initTitleBar()
{
    setMenuWidget(nullptr);
    titleBar = new TitleBar(this);
    titleBar->raise();
    setMenuWidget(titleBar);

    connect(titleBar, &TitleBar::signalButtonMinClicked, this, &MainWindow::onButtonMinClicked);
    connect(titleBar, &TitleBar::signalButtonRestoreClicked, this, &MainWindow::onButtonRestoreClicked);
    connect(titleBar, &TitleBar::signalButtonMaxClicked, this, &MainWindow::onButtonMaxClicked);
    connect(titleBar, &TitleBar::signalButtonCloseClicked, this, &MainWindow::onButtonCloseClicked);
}

void MainWindow::initStatusBar()
{
    statusBar = new StatusBar(this);
    setStatusBar(statusBar);
}

void MainWindow::initCenterContext()
{
     splitter = new QSplitter(Qt::Horizontal, this);

    // 左侧控件
    QWidget* widget = new QWidget(splitter);
    QVBoxLayout* layout = new QVBoxLayout(widget);
    layout->setContentsMargins(0, 0, 0, 0);  // 关键：移除布局边距
    layout->setSpacing(0);                   // 关键：移除控件间距

    QLabel* label = new QLabel("Hello World", widget);
    label->setAlignment(Qt::AlignCenter);     // 居中显示文本

    // 确保 QLabel 背景透明以显示父部件的黑色背景
    label->setStyleSheet("background: transparent; color: white;");

    layout->addWidget(label);
    widget->setLayout(layout);

    // 右侧控件
    QWidget* widget2 = new QWidget(splitter);

    // 将控件添加到 Splitter
    splitter->addWidget(widget);
    splitter->addWidget(widget2);

    // 设置伸缩比例
    splitter->setStretchFactor(0, 2);  // 左侧占2份
    splitter->setStretchFactor(1, 8);  // 右侧占8份

    // 设置初始大小（可选，确保比例可见）
    splitter->setSizes({200, 800});    // 左侧200px，右侧800px

    // 设置中央控件
    setCentralWidget(splitter);

    // 样式表
    splitter->setStyleSheet(R"(
        QSplitter::handle {
            background: #cccccc;
            width: 3px;
        }
        QSplitter::handle:hover {
            background: #aaaaaa;
        }
    )");

    widget->setStyleSheet(R"(
        QWidget {
            background: black;
        }
    )");
}

void MainWindow::onButtonMinClicked()
{
    if (Qt::Tool == (windowFlags() & Qt::Tool))
    {
        hide(); // 是否设置了Qt::Tool标志来决定是隐藏窗口还是调用showMinimized()来最小化窗口
    }
    else
    {
        showMinimized();
    }
}

void MainWindow::onButtonRestoreClicked()
{
    QPoint windowPos;
    QSize windowSize;
    titleBar->getRestoreInfo(windowPos, windowSize);
    this->setGeometry(QRect(windowPos, windowSize));
    Window::setIsWindowMax(false);
}

void MainWindow::onButtonMaxClicked()
{
    titleBar->saveRestoreInfo(this->pos(), QSize(this->width(), this->height()));
    // QRect desktopRect = QGuiApplication::primaryScreen()->availableGeometry();
    // QRect FactRect = QRect(desktopRect.x(), desktopRect.y(), desktopRect.width(), desktopRect.height());
    // setGeometry(FactRect);
    QScreen *screen = QGuiApplication::screenAt(this->pos());
    if (screen)
    {
        QRect desktopRect = screen->availableGeometry();
        setGeometry(desktopRect);
        Window::setIsWindowMax(true);
    }
    // showMaximized();
}

void MainWindow::onButtonCloseClicked()
{
    close();
}

// 初始化停靠窗口
void MainWindow::initDockWidget()
{
}